﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STOCK.DAL;
using System.Data.SqlClient;
using STOCK.Model;
using System.Data;

namespace STOCK.DAL
{
   public class T_CategoryDAL
    {
       public Guid GetCatIdByName(string name)
       {
           return (Guid)SqlHelper.ExecuteScalar("select Id from T_Category where Name=@Name",
               new SqlParameter("@Name", name));
       }
       public T_Category[] ListAll()
       {
           DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Category");
           return ToT_Category(dt);
       }
       public T_Category ToModel(DataRow row)
       {
           T_Category cate = new T_Category();
           cate.Id = (Guid)row["Id"];
           cate.Name = (string)row["Name"];
           return cate;
       }

       private T_Category[] ToT_Category(DataTable dt)
       {
           T_Category[] items = new T_Category[dt.Rows.Count];
           for (int i = 0; i < dt.Rows.Count; i++)
           {
               T_Category cate = new T_Category();
               cate = ToModel(dt.Rows[i]);
               items[i] = cate;
           }
           return items;
       }
    }
}
